#include <game.h>
#include <map.h>

int width = 0;
int height = 0;
int off_x = 0;
int off_y = 0;

uint32_t pix_avatar[] = {
    0, 0, 0, 0 ,0, 0, 0, 0, 0, 0, 0, 0, 0xffffff, 0xffffff, 0xffffff, 0xffffff, 0xffffff, 0xffffff, 0xffffff, 0xffffff, 0, 
    0, 0, 0, 0 ,0, 0, 0, 0, 0, 0, 0, 0xffffff, 0xffffff, 0xffffff, 0xffffff, 0xffffff, 0xffffff, 0xffffff, 0xffffff, 0xffffff, 0xffffff, 
    0, 0, 0, 0 ,0, 0, 0, 0, 0, 0, 0, 0xffffff, 0xffffff, 0xffffff, 0xffffff, 0xffffff, 0xffffff, 0xffffff, 0xffffff, 0xffffff, 0xffffff, 
    0, 0, 0, 0 ,0, 0, 0, 0, 0, 0, 0, 0xffffff, 0xffffff, 0, 0xffffff, 0xffffff, 0xffffff, 0xffffff, 0xffffff, 0xffffff, 0xffffff, 
    0, 0, 0, 0 ,0, 0, 0, 0, 0, 0, 0, 0xffffff, 0xffffff, 0xffffff, 0xffffff, 0xffffff, 0xffffff, 0xffffff, 0xffffff, 0xffffff, 0xffffff, 
    0, 0, 0, 0 ,0, 0, 0, 0, 0, 0, 0, 0xffffff, 0xffffff, 0xffffff, 0xffffff, 0xffffff, 0xffffff, 0xffffff, 0xffffff, 0xffffff, 0xffffff, 
    0, 0, 0, 0 ,0, 0, 0, 0, 0, 0, 0, 0xffffff, 0xffffff, 0xffffff, 0xffffff, 0xffffff, 0, 0, 0, 0, 0, 
    0, 0, 0, 0 ,0, 0, 0, 0, 0, 0, 0, 0xffffff, 0xffffff, 0xffffff, 0xffffff, 0xffffff, 0xffffff, 0xffffff, 0xffffff, 0, 0,
    0, 0xffffff, 0, 0 ,0, 0, 0, 0, 0, 0, 0xffffff, 0xffffff, 0xffffff, 0xffffff, 0xffffff, 0, 0, 0, 0, 0, 0, 
    0, 0xffffff, 0, 0 ,0, 0, 0, 0, 0, 0xffffff, 0xffffff, 0xffffff, 0xffffff, 0xffffff, 0xffffff, 0, 0, 0, 0, 0, 0, 
    0, 0xffffff, 0xffffff, 0 ,0, 0, 0, 0, 0xffffff, 0xffffff, 0xffffff, 0xffffff, 0xffffff, 0xffffff, 0xffffff, 0xffffff, 0xffffff, 0, 0, 0, 0,
    0, 0xffffff, 0xffffff, 0xffffff ,0, 0, 0, 0xffffff, 0xffffff, 0xffffff, 0xffffff, 0xffffff, 0xffffff, 0xffffff, 0xffffff, 0, 0xffffff, 0, 0, 0, 0, 
    0, 0xffffff, 0xffffff, 0xffffff ,0xffffff, 0xffffff, 0xffffff, 0xffffff, 0xffffff, 0xffffff, 0xffffff, 0xffffff, 0xffffff, 0xffffff, 0xffffff, 0, 0, 0, 0, 0, 0,
    0, 0xffffff, 0xffffff, 0xffffff ,0xffffff, 0xffffff, 0xffffff, 0xffffff, 0xffffff, 0xffffff, 0xffffff, 0xffffff, 0xffffff, 0xffffff, 0xffffff, 0, 0, 0, 0, 0, 0,
    0, 0, 0xffffff, 0xffffff ,0xffffff, 0xffffff, 0xffffff, 0xffffff, 0xffffff, 0xffffff, 0xffffff, 0xffffff, 0xffffff, 0xffffff, 0, 0, 0, 0, 0, 0, 0,
    0, 0, 0, 0xffffff ,0xffffff, 0xffffff, 0xffffff, 0xffffff, 0xffffff, 0xffffff, 0xffffff, 0xffffff, 0xffffff, 0, 0, 0, 0, 0, 0, 0, 0, 
    0, 0, 0, 0 ,0xffffff, 0xffffff, 0xffffff, 0xffffff, 0xffffff, 0xffffff, 0xffffff, 0xffffff, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
    0, 0, 0, 0 ,0, 0xffffff, 0xffffff, 0xffffff, 0xffffff, 0xffffff, 0xffffff, 0xffffff, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
    0, 0, 0, 0 ,0, 0, 0xffffff, 0, 0, 0, 0, 0xffffff, 0xffffff, 0, 0, 0, 0, 0, 0, 0, 0, 
    0, 0, 0, 0 ,0, 0, 0xffffff, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
    0, 0, 0, 0 ,0, 0, 0xffffff, 0xffffff, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
};


uint32_t pix_avatar_l[] = {
    0, 0, 0, 0 ,0, 0, 0, 0, 0, 0, 0, 0, 0xffffff, 0xffffff, 0xffffff, 0xffffff, 0xffffff, 0xffffff, 0xffffff, 0xffffff, 0,
    0, 0, 0, 0 ,0, 0, 0, 0, 0, 0, 0, 0xffffff, 0xffffff, 0xffffff, 0xffffff, 0xffffff, 0xffffff, 0xffffff, 0xffffff, 0xffffff, 0xffffff, 
    0, 0, 0, 0 ,0, 0, 0, 0, 0, 0, 0, 0xffffff, 0xffffff, 0xffffff, 0xffffff, 0xffffff, 0xffffff, 0xffffff, 0xffffff, 0xffffff, 0xffffff, 
    0, 0, 0, 0 ,0, 0, 0, 0, 0, 0, 0, 0xffffff, 0xffffff, 0, 0xffffff, 0xffffff, 0xffffff, 0xffffff, 0xffffff, 0xffffff, 0xffffff, 
    0, 0, 0, 0 ,0, 0, 0, 0, 0, 0, 0, 0xffffff, 0xffffff, 0xffffff, 0xffffff, 0xffffff, 0xffffff, 0xffffff, 0xffffff, 0xffffff, 0xffffff, 
    0, 0, 0, 0 ,0, 0, 0, 0, 0, 0, 0, 0xffffff, 0xffffff, 0xffffff, 0xffffff, 0xffffff, 0xffffff, 0xffffff, 0xffffff, 0xffffff, 0xffffff, 
    0, 0, 0, 0 ,0, 0, 0, 0, 0, 0, 0, 0xffffff, 0xffffff, 0xffffff, 0xffffff, 0xffffff, 0, 0, 0, 0, 0, 
    0, 0, 0, 0 ,0, 0, 0, 0, 0, 0, 0, 0xffffff, 0xffffff, 0xffffff, 0xffffff, 0xffffff, 0xffffff, 0xffffff, 0xffffff, 0, 0, 
    0, 0, 0, 0 ,0, 0, 0, 0, 0, 0, 0xffffff, 0xffffff, 0xffffff, 0xffffff, 0xffffff, 0, 0, 0, 0, 0, 0, 
    0, 0, 0, 0 ,0, 0, 0, 0, 0, 0xffffff, 0xffffff, 0xffffff, 0xffffff, 0xffffff, 0xffffff, 0, 0, 0, 0, 0, 0, 
    0xffffff, 0, 0, 0 ,0, 0, 0, 0, 0xffffff, 0xffffff, 0xffffff, 0xffffff, 0xffffff, 0xffffff, 0xffffff, 0xffffff, 0xffffff, 0, 0, 0, 0, 
    0xffffff, 0xffffff, 0, 0 ,0, 0, 0, 0xffffff, 0xffffff, 0xffffff, 0xffffff, 0xffffff, 0xffffff, 0xffffff, 0xffffff, 0, 0xffffff, 0, 0, 0, 0, 0,
    0xffffff, 0xffffff, 0xffffff, 0xffffff ,0xffffff, 0xffffff, 0xffffff, 0xffffff, 0xffffff, 0xffffff, 0xffffff, 0xffffff, 0xffffff, 0xffffff, 0xffffff, 0, 0, 0, 0, 0, 0, 
    0xffffff, 0xffffff, 0xffffff, 0xffffff ,0xffffff, 0xffffff, 0xffffff, 0xffffff, 0xffffff, 0xffffff, 0xffffff, 0xffffff, 0xffffff, 0xffffff, 0xffffff, 0, 0, 0, 0, 0, 0, 
    0xffffff, 0xffffff, 0xffffff, 0xffffff ,0xffffff, 0xffffff, 0xffffff, 0xffffff, 0xffffff, 0xffffff, 0xffffff, 0xffffff, 0xffffff, 0xffffff, 0, 0, 0, 0, 0, 0, 0, 
    0, 0xffffff, 0xffffff, 0xffffff ,0xffffff, 0xffffff, 0xffffff, 0xffffff, 0xffffff, 0xffffff, 0xffffff, 0xffffff, 0xffffff, 0, 0, 0, 0, 0, 0, 0, 0, 
    0, 0, 0xffffff, 0xffffff, 0xffffff, 0xffffff, 0xffffff, 0xffffff, 0xffffff, 0xffffff, 0xffffff, 0xffffff, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
    0, 0, 0, 0xffffff ,0xffffff, 0xffffff, 0xffffff, 0xffffff, 0xffffff, 0xffffff, 0xffffff, 0xffffff, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
    0, 0, 0, 0 ,0xffffff, 0xffffff, 0xffffff, 0xffffff, 0, 0, 0, 0xffffff, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
    0, 0, 0, 0 ,0, 0, 0, 0, 0, 0, 0, 0xffffff, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
    0, 0, 0, 0 ,0, 0, 0, 0, 0, 0, 0, 0xffffff, 0xffffff, 0, 0, 0, 0, 0, 0, 0, 0
};

uint32_t pix_avatar_r[] = {
    0, 0, 0, 0 ,0, 0, 0, 0, 0, 0, 0, 0, 0xffffff, 0xffffff, 0xffffff, 0xffffff, 0xffffff, 0xffffff, 0xffffff, 0xffffff, 0, 
    0, 0, 0, 0 ,0, 0, 0, 0, 0, 0, 0, 0xffffff, 0xffffff, 0xffffff, 0xffffff, 0xffffff, 0xffffff, 0xffffff, 0xffffff, 0xffffff, 0xffffff, 
    0, 0, 0, 0 ,0, 0, 0, 0, 0, 0, 0, 0xffffff, 0xffffff, 0xffffff, 0xffffff, 0xffffff, 0xffffff, 0xffffff, 0xffffff, 0xffffff, 0xffffff, 
    0, 0, 0, 0 ,0, 0, 0, 0, 0, 0, 0, 0xffffff, 0xffffff, 0, 0xffffff, 0xffffff, 0xffffff, 0xffffff, 0xffffff, 0xffffff, 0xffffff, 
    0, 0, 0, 0 ,0, 0, 0, 0, 0, 0, 0, 0xffffff, 0xffffff, 0xffffff, 0xffffff, 0xffffff, 0xffffff, 0xffffff, 0xffffff, 0xffffff, 0xffffff, 
    0, 0, 0, 0 ,0, 0, 0, 0, 0, 0, 0, 0xffffff, 0xffffff, 0xffffff, 0xffffff, 0xffffff, 0xffffff, 0xffffff, 0xffffff, 0xffffff, 0xffffff, 
    0, 0, 0, 0 ,0, 0, 0, 0, 0, 0, 0, 0xffffff, 0xffffff, 0xffffff, 0xffffff, 0xffffff, 0, 0, 0, 0, 0, 
    0, 0, 0, 0 ,0, 0, 0, 0, 0, 0, 0, 0xffffff, 0xffffff, 0xffffff, 0xffffff, 0xffffff, 0xffffff, 0xffffff, 0xffffff, 0, 0,
    0, 0, 0, 0 ,0, 0, 0, 0, 0, 0, 0xffffff, 0xffffff, 0xffffff, 0xffffff, 0xffffff, 0, 0, 0, 0, 0, 0, 
    0, 0xffffff, 0, 0 ,0, 0, 0, 0, 0, 0xffffff, 0xffffff, 0xffffff, 0xffffff, 0xffffff, 0xffffff, 0, 0, 0, 0, 0, 0, 
    0, 0xffffff, 0xffffff, 0 ,0, 0, 0, 0, 0xffffff, 0xffffff, 0xffffff, 0xffffff, 0xffffff, 0xffffff, 0xffffff, 0xffffff, 0xffffff, 0, 0, 0, 0,
    0, 0xffffff, 0xffffff, 0xffffff ,0, 0, 0, 0xffffff, 0xffffff, 0xffffff, 0xffffff, 0xffffff, 0xffffff, 0xffffff, 0xffffff, 0, 0xffffff, 0, 0, 0, 0, 
    0, 0xffffff, 0xffffff, 0xffffff ,0xffffff, 0xffffff, 0xffffff, 0xffffff, 0xffffff, 0xffffff, 0xffffff, 0xffffff, 0xffffff, 0xffffff, 0xffffff, 0, 0, 0, 0, 0, 0,
    0, 0xffffff, 0xffffff, 0xffffff ,0xffffff, 0xffffff, 0xffffff, 0xffffff, 0xffffff, 0xffffff, 0xffffff, 0xffffff, 0xffffff, 0xffffff, 0xffffff, 0, 0, 0, 0, 0, 0,
    0, 0, 0xffffff, 0xffffff ,0xffffff, 0xffffff, 0xffffff, 0xffffff, 0xffffff, 0xffffff, 0xffffff, 0xffffff, 0xffffff, 0xffffff, 0, 0, 0, 0, 0, 0, 0,
    0, 0, 0, 0xffffff ,0xffffff, 0xffffff, 0xffffff, 0xffffff, 0xffffff, 0xffffff, 0xffffff, 0xffffff, 0xffffff, 0, 0, 0, 0, 0, 0, 0, 0, 
    0, 0, 0, 0 ,0xffffff, 0xffffff, 0xffffff, 0xffffff, 0xffffff, 0xffffff, 0xffffff, 0xffffff, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
    0, 0, 0, 0 ,0, 0xffffff, 0xffffff, 0xffffff, 0xffffff, 0xffffff, 0xffffff, 0xffffff, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
    0, 0, 0, 0 ,0, 0, 0xffffff, 0, 0, 0, 0, 0xffffff, 0xffffff, 0, 0, 0, 0, 0, 0, 0, 0, 
    0, 0, 0, 0 ,0, 0, 0xffffff, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
    0, 0, 0, 0 ,0, 0, 0xffffff, 0xffffff, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
};

uint32_t pix_avatar_b[AVATAR_SZ*AVATAR_SZ] = {0};

Block blocks[LVL_NUM][BLOCK_NUM] = {

    {
        // L 8
        {10, 20}, {10, 40}, {10, 60}, {10, 80}, {10, 100}, {20, 100}, {30, 100}, {40, 100}, 
        // // A 12
        {60, 40}, {60, 60}, {60, 80}, {60, 100}, {70, 20}, {90, 40}, {90, 60}, {90, 80}, {90, 100}, {70, 60}, {80, 60}, {80, 20},
        // // B 19
        {110, 20}, {110, 40}, {110, 60}, {110, 80}, {110, 100}, {120, 20}, {120, 60}, {120, 100}, {150, 40}, {150, 60}, {150, 80}, {130, 20}, {130, 60}, {130, 100}, {140, 20}, {140, 60}, {140, 100}, 
        // // 0 16
        {190, 20}, {190, 40}, {190, 60}, {190, 80}, {190, 100}, {220, 20}, {220, 40}, {220, 60}, {220, 80}, {220, 100}, {200, 20}, {200, 100}, {210, 20}, {210, 100},
        // bbb 2
        {120, GROUND}, {130, GROUND}, {270, GROUND},{320, GROUND}, {280, GROUND}, {290, GROUND}, {300, GROUND}, {310, GROUND}, {280, GROUND-BLOCK_HEIGHT}, {290, GROUND-BLOCK_HEIGHT}, {300, GROUND-BLOCK_HEIGHT}, {310, GROUND-BLOCK_HEIGHT}, 

    }, 
    {
        {80, GROUND-70}, {80, GROUND-90}, {80, GROUND-110}, {80, GROUND-130}, {120, GROUND}, {130, GROUND}, {140, GROUND}, {150, GROUND}, {120, GROUND-20}, {130, GROUND-20}, {140, GROUND-20}, {150, GROUND-20}, 
        {240, GROUND}, {240, GROUND-70}, {240, GROUND-90}, {240, GROUND-110}, {240, GROUND-130}, 
        {370, GROUND}, {380, GROUND}, {390, GROUND}, {360, GROUND}, {370, GROUND-40}, {380, GROUND-40}, {390, GROUND-20}, {360, GROUND-40}
    }, 
    {
        {100, GROUND}, {110, GROUND}, {120, GROUND}, {130, GROUND}, {140, GROUND}, 
        {100, GROUND-30}, {110, GROUND-30}, {120, GROUND-30}, {130, GROUND-30}, {140, GROUND-30}, 
        {230, GROUND}, {240, GROUND}, {250, GROUND}, {260, GROUND}, {270, GROUND},
        {230, GROUND-30}, {240, GROUND-30}, {250, GROUND-30}, {260, GROUND-30}, {270, GROUND-30}, 
        {360, GROUND}, {370, GROUND}, {380, GROUND}, {390, GROUND}, {400, GROUND},
        {360, GROUND-30}, {370, GROUND-30}, {380, GROUND-30}, {390, GROUND-30}, {400, GROUND-30},
    }, 
    {
        {140, GROUND}, {230, GROUND}, {230, GROUND-20}, {240, GROUND-80}, {390, GROUND}, {390, GROUND-30}, {380, GROUND}, {380, GROUND-30}, {370, GROUND}, {370, GROUND-30}, {360, GROUND}, {360, GROUND-30}
    }

};

uint32_t pix_block[] = {
    0, 0, 0, 0, 0xffffff, 0xffffff, 0, 0, 0, 0,
    0, 0, 0, 0, 0xffffff, 0xffffff, 0, 0, 0, 0,
    0, 0, 0, 0, 0xffffff, 0xffffff, 0, 0, 0, 0,
    0xffffff, 0xffffff, 0, 0, 0xffffff, 0xffffff, 0, 0, 0, 0,
    0xffffff, 0xffffff, 0, 0, 0xffffff, 0xffffff, 0, 0, 0, 0,
    0xffffff, 0xffffff, 0, 0, 0xffffff, 0xffffff, 0, 0, 0, 0,
    0xffffff, 0xffffff, 0, 0, 0xffffff, 0xffffff, 0, 0xffffff, 0xffffff, 0,
    0xffffff, 0xffffff, 0, 0, 0xffffff, 0xffffff, 0, 0xffffff, 0xffffff, 0,
    0xffffff, 0xffffff, 0, 0, 0xffffff, 0xffffff, 0, 0xffffff, 0xffffff, 0,
    0xffffff, 0xffffff, 0, 0, 0xffffff, 0xffffff, 0, 0xffffff, 0xffffff, 0,
    0xffffff, 0xffffff, 0xffffff, 0xffffff, 0xffffff, 0xffffff, 0, 0xffffff, 0xffffff, 0,
    0, 0, 0, 0, 0xffffff, 0xffffff, 0, 0xffffff, 0xffffff, 0,
    0, 0, 0, 0, 0xffffff, 0xffffff, 0xffffff, 0xffffff, 0xffffff, 0,
    0, 0, 0, 0, 0xffffff, 0xffffff, 0, 0, 0, 0,
    0, 0, 0, 0, 0xffffff, 0xffffff, 0, 0, 0, 0,
    0, 0, 0, 0, 0xffffff, 0xffffff, 0, 0, 0, 0,
    0, 0, 0, 0, 0xffffff, 0xffffff, 0, 0, 0, 0,
    0, 0, 0, 0, 0xffffff, 0xffffff, 0, 0, 0, 0,
    0, 0, 0, 0, 0xffffff, 0xffffff, 0, 0, 0, 0,
    0, 0, 0, 0, 0xffffff, 0xffffff, 0, 0, 0, 0,
};

uint32_t pix_block_b[] = {0};

void init_map() {
    width = screen_width();
    height = screen_height();
    off_x = (width-400)/2;
    off_y = (height-300)/2;
    for (int i=0; i<LVL_NUM; i++) {
        for (int j=0; j<BLOCK_NUM; j++) {
            blocks[i][j].x += off_x;
            blocks[i][j].y += off_y;
        }
    }
    // for (int i=0; i<sizeof(pix_block)/sizeof(uint32_t); i++) {
    //     if (pix_block[i]==0) pix_block[i] = 0x63b8ff;
    //     else pix_block[i] = 0x6b8e23;
    // }

    // for (int i=0; i<sizeof(pix_avatar)/sizeof(uint32_t); i++) {
    //     if (pix_avatar[i])
    //         pix_avatar[i] = 0xcdc0b0;
    //     else pix_avatar[i] = 0x63b8ff;
    //     if (pix_avatar_l[i])
    //         pix_avatar_l[i] = 0xcdc0b0;
    //     else pix_avatar_l[i] = 0x63b8ff;
    //     if (pix_avatar_r[i])
    //         pix_avatar_r[i] = 0xcdc0b0;
    //     else pix_avatar_r[i] = 0x63b8ff;
    // }
    // printf("%d, %d\n", width, height);
}

void draw_avatar() {
    static int pos = -1;
    if (avatar.flying) {
        draw_rect(pix_avatar, avatar.x, avatar.y, AVATAR_SZ, AVATAR_SZ);
        pos = -1;
    }
    else {
        if (pos == -1) {
            pos = 0;
        }
        if (pos<10) {
            draw_rect(pix_avatar_l, avatar.x, avatar.y, AVATAR_SZ, AVATAR_SZ);
            pos += 1;
        }
        else {
            draw_rect(pix_avatar_r, avatar.x, avatar.y, AVATAR_SZ, AVATAR_SZ);
            pos += 1;
        }
        pos %= 20;
    }
};

void undraw_avatar() {
    draw_rect(pix_avatar_b, avatar.x, avatar.y, AVATAR_SZ, AVATAR_SZ);
}

void draw_map(int lvl) {
    for (int i=0; i<BLOCK_NUM; i++) {
        if (blocks[lvl][i].x == off_x && blocks[lvl][i].y==off_y) break;
        draw_rect(pix_block, blocks[lvl][i].x, blocks[lvl][i].y, BLOCK_WIDTH, BLOCK_HEIGHT);
    }
};

void undraw_map(int lvl) {
    for (int i=0; i<BLOCK_NUM; i++) {
        draw_rect(pix_block_b, blocks[lvl][i].x, blocks[lvl][i].y, BLOCK_WIDTH, BLOCK_HEIGHT);
    }
};

void draw_bg() {
    uint32_t pixel[1960];
    // for (int i = 0; i<width; i++) pixel[i] = 0x63B8FF;
    // for (int j=0; j<height; j++){
    //     draw_rect(pixel, 0, j, width, 1);
    // }
    for (int i=0; i<2*width; i++) pixel[i] = 0xffffff;
    draw_rect(pixel, 0, GROUND+AVATAR_SZ+off_y, width, 2);
}
